package ru.spb.osll.myfit.client.widget;

import java.util.Iterator;
import java.util.List;

import ru.spb.osll.myfit.client.PageMode;
import ru.spb.osll.myfit.common.Content;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageElementType;
import ru.spb.osll.myfit.common.TreeNode;

import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

/**
 * Created by IntelliJ IDEA. User: Vassar Date: 09.03.2011 Time: 17:41:37 To
 * change this template use File | Settings | File Templates.
 */
public class ContentWidget extends PageElementWidget {
  private TreeNode m_treeNode;
  private PageElementType m_type;
  private final int m_level;
  private String m_prefix;

  public ContentWidget(GWTWidgetFactory gwtWidgetFactory, Content content) {
    super(gwtWidgetFactory);
    sinkEvents(Event.ONMOUSEUP | Event.ONDBLCLICK | Event.ONCONTEXTMENU);
    createPopupMenu();

    m_treeNode = content.getTreeNode();
    m_type = content.getType();
    m_level = content.getLevel();
    m_prefix = content.getPath();
    initWidget(createWidget());
    m_mode = PageMode.EDIT;
  }

  protected Widget createWidget() {

    FlexTable layout = m_gwtWidgetFactory.createFlexTable();
    layout.setCellSpacing(6);
    Tree tree = new Tree();

    if (m_treeNode != null) {
      List<TreeNode> children = m_treeNode.getChildren();
      Iterator<TreeNode> iterator = children.iterator();

      while (iterator.hasNext()) {
        TreeItem item = getTreeItem(iterator.next(), m_prefix);
        tree.addItem(item);
      }
    }

    layout.setWidget(0, 0, tree);
    DecoratorPanel panel = m_gwtWidgetFactory.createDecoratorPanel();
    panel.setWidget(layout);

    return panel;
  }

  private TreeItem getTreeItem(TreeNode root, String prefix) {
    String pagePath;
    String name = root.getReference();

    if (!prefix.isEmpty())
      pagePath = prefix + "." + name;
    else
      pagePath = name;

    if ((root.getChildren() != null) && (root.getChildren().isEmpty()))
	name += "...";

    String reference = "<a href=\"" +
			pagePath + "\">" +
			name + "</a>";

    TreeItem outerRoot =
	new TreeItem(new HTML(reference));

    if (root.getChildren() != null)
      addChildren(root.getChildren(), outerRoot, pagePath);

    return outerRoot;
  }

  private void addChildren(List<TreeNode> children, TreeItem root, String prefix) {
    Iterator<TreeNode> iterator = children.iterator();

    while (iterator.hasNext()) {
      TreeItem item = getTreeItem(iterator.next(), prefix);
      root.addItem(item);
    }
  }

  @Override
  public PageElement getPageElement() {
    return new Content(m_level);
  }

  @Override
  public void setOpen(boolean isOpen) {
    // To change body of implemented methods use File | Settings | File
    // Templates.
  }

  @Override
  public PageElementType getType() {
    return m_type;
  }
}
